/*******************************************************************************
* Purpose: Preprocess questions regarding daycare @ 18m, 24m, 30m and 36m
* 
* Dependencies:
*   Input: DSC0051.dta, 18-months family interview
*		   DSC0061.dta, 24-months family interview
*		   DSC0081.dta, 30-months family interview
*		   DSC0089.dta, 36-months family interview
*   Output: daycare_inputs.dta
*
* Notes: -
*******************************************************************************/
set more off
clear all

global storage: env storage

use "$storage/ece_parenting/raw_data/DSC0001.dta"
keep IHDP PAG

// F28, 18-Month Family Interview
merge 1:1 IHDP using "$storage/ece_parenting/raw_data/DSC0051.dta", generate(_mergeF28)
drop V14c2_18m V16c2_18m V27_18m V27b_18m V29a_18m V29b_18m
// F34, 24-Month Family Interview
merge 1:1 IHDP using "$storage/ece_parenting/raw_data/DSC0061.dta", generate(_mergeF34)
drop V13c2_24m V15c2_24m V19_24m V19b_24m V20a_24m V20b_24m
// F47, 30-Month Family Interview
merge 1:1 IHDP using "$storage/ece_parenting/raw_data/DSC0081.dta", generate(_mergeF47)
drop V16_30m V17_30m V18c2_30m V23 V24 V20c2_30m V29_30m V29b_30m V30a_30m V30b_30m
// F52, 36-Month Family Interview
merge 1:1 IHDP using "$storage/ece_parenting/raw_data/DSC0089.dta", generate(_mergeF52)
drop V29c1_36m V38c1_36m V58_36m V58b_36m V59a_36m V59b_36m

// keep primary sample
drop if PAG == 0

////////////////////////////////////////////////////////////////////////////////
////////////////////////// Summarize hours @ 18m ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// hours in daycare
replace V15b_18m = 0 if V15b_18m == .
replace V15c_18m = 0 if V15c_18m == .
replace V17b_18m = 0 if V17b_18m == .
replace V17c_18m = 0 if V17c_18m == .

// hours in other (non-maternal) care
replace V14b_18m  = 0 if V14b_18m == .
replace V14c1_18m = 0 if V14c1_18m == .
replace V16b_18m  = 0 if V16b_18m == .
replace V16c1_18m = 0 if V16c1_18m == .

// daycare hours (primary + secondary)
gen hours_daycare_18m = V15b_18m * V15c_18m
gen hours_daycare_18m_secondary = V17b_18m * V17c_18m

// non-maternal non-daycare care hours (primary + secondary)
gen hours_othercare_18m = V14b_18m * V14c1_18m
gen hours_othercare_18m_secondary = V16b_18m * V16c1_18m

drop V14_18m V14a_18m V14b_18m V14c1_18m V15b_18m V15c_18m ///
     V16_18m V16a_18m V16b_18m V16c1_18m V17b_18m V17c_18m ///
	 V19_18m V21_18m _mergeF28
	 
////////////////////////////////////////////////////////////////////////////////
////////////////////////// Summarize hours @ 24m ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// hours in daycare
replace V14b_24m = 0 if V14b_24m == .
replace V14c_24m = 0 if V14c_24m == .
replace V16b_24m = 0 if V16b_24m == .
replace V16c_24m = 0 if V16c_24m == .

// hours in other (non-maternal) care
replace V13b_24m  = 0 if V13b_24m == .
replace V13c1_24m = 0 if V13c1_24m == .
replace V15b_24m  = 0 if V15b_24m == .
replace V15c1_24m = 0 if V15c1_24m == .

// daycare hours (primary + secondary)
gen hours_daycare_24m 				= V14b_24m * V14c_24m
gen hours_daycare_24m_secondary 	= V16b_24m * V16c_24m

// non-maternal non-daycare care hours (primary + secondary)
gen hours_othercare_24m 			= V13b_24m * V13c1_24m
gen hours_othercare_24m_secondary 	= V15b_24m * V15c1_24m

drop V13_24m V13a_24m V13b_24m V13c1_24m V14b_24m V14c_24m ///
     V15_24m V15a_24m V15b_24m V15c1_24m V16b_24m V16c_24m _mergeF34

////////////////////////////////////////////////////////////////////////////////
////////////////////////// Summarize hours @ 30m ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// hours in daycare
replace V19b_30m = 0 if V19b_30m == .
replace V19c_30m = 0 if V19c_30m == .
replace V21b_30m = 0 if V21b_30m == .
replace V21c_30m = 0 if V21c_30m == .

// hours in other (non-maternal) care
replace V18b_30m  = 0 if V18b_30m == .
replace V18c1_30m = 0 if V18c1_30m == .
replace V20b_30m  = 0 if V20b_30m == .
replace V20c1_30m = 0 if V20c1_30m == .

// daycare hours (primary + secondary)
gen hours_daycare_30m = V19b_30m * V19c_30m
gen hours_daycare_30m_secondary = V21b_30m * V21c_30m

// non-maternal non-daycare care hours (primary + secondary)
gen hours_othercare_30m 			= V18b_30m * V18c1_30m
gen hours_othercare_30m_secondary 	= V20b_30m * V20c1_30m

drop V18_30m V18a_30m V18b_30m V18c1_30m V19b_30m V19c_30m ///
     V20_30m V20a_30m V20b_30m V20c1_30m V21b_30m V21c_30m _mergeF47  
	 
////////////////////////////////////////////////////////////////////////////////
////////////////////////// Summarize hours @ 36m ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// hours in daycare
replace V31b_36m = 0 if V31b_36m == .
replace V31c_36m = 0 if V31c_36m == .
replace V40b_36m = 0 if V40b_36m == .
replace V40c_36m = 0 if V40c_36m == .

// hours in other (non-maternal) care
replace V29b_36m = 0 if V29b_36m == .
replace V29c_36m = 0 if V29c_36m == .
replace V38b_36m = 0 if V38b_36m == .
replace V38c_36m = 0 if V38c_36m == .

// hours in family care
replace V30c_36m = 0 if V30c_36m == .
replace V30d_36m = 0 if V30d_36m == .
replace V39c_36m = 0 if V39c_36m == .
replace V39d_36m = 0 if V39d_36m == .

// daycare hours (primary + secondary)
gen hours_daycare_36m 				= V31b_36m * V31c_36m
gen hours_daycare_36m_secondary 	= V40b_36m * V40c_36m

// non-maternal non-daycare care hours (primary + secondary)
gen hours_othercare_36m 			= V29b_36m * V29c_36m
gen hours_othercare_36m_secondary 	= V38b_36m * V38c_36m

// family care hours (primary + secondary)
gen hours_familycare_36m 			= V30c_36m * V30d_36m
gen hours_familycare_36m_secondary 	= V39c_36m * V39d_36m

// add family care and other care
replace hours_othercare_36m = hours_othercare_36m + hours_familycare_36m
replace hours_othercare_36m_secondary = hours_othercare_36m_secondary + hours_familycare_36m_secondary
drop hours_familycare_36m hours_familycare_36m_secondary

drop V29_36m V29a_36m V29b_36m V29c_36m V30c_36m V30d_36m /// 
	 V31b_36m V31c_36m V38_36m V38a_36m V38b_36m V38c_36m ///
	 V39c_36m V39d_36m V40b_36m V40c_36m _mergeF52

////////////////////////////////////////////////////////////////////////////////
////////////////////////// Daycare Inputs //////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// average center-based daycare
gen avg_daycare_18m 	= hours_daycare_18m
egen avg_daycare_24m 	= rowmean(hours_daycare_24m)
egen avg_daycare_30m 	= rowmean(hours_daycare_30m)
egen avg_daycare_36m 	= rowmean(hours_daycare_36m)

// average daycare (secondary)
gen avg_daycare_18m_secondary 	= hours_daycare_18m_secondary 
egen avg_daycare_24m_secondary 	= rowmean(hours_daycare_24m_secondary)
egen avg_daycare_30m_secondary 	= rowmean(hours_daycare_30m_secondary)
egen avg_daycare_36m_secondary 	= rowmean(hours_daycare_36m_secondary)

// average of averages 
egen avg_daycare_18m_sum = rowtotal(avg_daycare_18m avg_daycare_18m_secondary)
egen avg_daycare_24m_sum = rowtotal(avg_daycare_24m avg_daycare_24m_secondary)
egen avg_daycare_30m_sum = rowtotal(avg_daycare_30m avg_daycare_30m_secondary)
egen avg_daycare_36m_sum = rowtotal(avg_daycare_36m avg_daycare_36m_secondary)

// generate final daycare measure
egen cum_avg_daycare_36m_sum = rowmean(avg_daycare_18m_sum avg_daycare_24m_sum avg_daycare_30m_sum avg_daycare_36m_sum)
lab var cum_avg_daycare_36m_sum "Final Daycare Measure"

// drop auxilary variables
drop hours_daycare_18m hours_daycare_18m_secondary hours_daycare_24m ///
     hours_daycare_24m_secondary hours_daycare_30m ///
     hours_daycare_30m_secondary hours_daycare_36m hours_daycare_36m_secondary  ///
     avg_daycare_18m avg_daycare_24m avg_daycare_30m avg_daycare_36m ///
	 avg_daycare_18m_secondary avg_daycare_24m_secondary ///
     avg_daycare_30m_secondary avg_daycare_36m_secondary avg_daycare_18m_sum ///
     avg_daycare_24m_sum avg_daycare_30m_sum avg_daycare_36m_sum ///
	 hours_othercare_18m hours_othercare_18m_secondary hours_othercare_24m ///
	 hours_othercare_24m_secondary hours_othercare_30m ///
	 hours_othercare_30m_secondary hours_othercare_36m hours_othercare_36m_secondary

capture mkdir "$storage/ece_parenting/intermediate_data/inputs"
save "$storage/ece_parenting/intermediate_data/inputs/daycare_inputs.dta", replace
